package com.chen.controller;


import com.chen.common.Result;
import com.chen.entity.Request;
import com.chen.entity.User;
import com.chen.mapper.UserMapper;
import com.chen.service.IRequestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Y·C
 * @since 2023-04-21
 */
@Api(tags = "择偶要求")
@RestController
@RequestMapping("/request")
public class RequestController {

    @Resource
    private IRequestService requestService;

    @Resource
    private UserMapper userMapper;

    @ApiOperation("填写择偶信息接口 id、creat_time、uid不用填写")
    @PostMapping("/insertRequest")
    public Result requestInfo(@RequestBody Request request) {
        requestService.requestInfo(request);
        return Result.success();
    }

    @ApiOperation("修改择偶信息接口 id、creat_time不用填写 uid传当前用户id")
    @PostMapping("/updateRequest")
    public Result updateRequestInfo(@RequestBody Request request) {
        requestService.updateRequestInfo(request);
        return Result.success();
    }

    @ApiOperation("首页展示接口（根据择偶信息展示的） 传id就行")
    @PostMapping("/select/{id}")
    public List<User> select(@PathVariable Integer id) {
        List<User> list = new ArrayList<>();
        User user = userMapper.selectById(id);
        Request request = requestService.queryRequest(user.getId());
//        System.out.println(user.getSex().equals("女"));
        Set map = new HashSet();
        if ("女".equals(user.getSex())){
            List<User> sex = requestService.querySex("男");
            for (User sex1 : sex) {
                map.add(sex1.getId());
            }
        }else {
            List<User> sex = requestService.querySex("女");
            for (User sex1 : sex) {
                map.add(sex1.getId());
            }
        }
        if (!"".equals(request.getMaritalStatus())) {
            List<User> users = requestService.queryWedding(request.getMaritalStatus());
            for (User user1 : users) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getMinAge() != null && request.getMaxAge() != null) {
            List<User> users0 = requestService.queryAge(request.getMinAge(), request.getMaxAge());
            for (User user1 : users0) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getMinHeight() != null && request.getMaxHeight() != null) {
            List<User> users1 = requestService.queryHeight(request.getMinHeight(), request.getMaxHeight());
            for (User user1 : users1) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (!"".equals(request.getHouse())) {
            List<User> users2 = requestService.queryHouse(request.getHouse());
            for (User user1 : users2) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (!"".equals(request.getCart())) {
            List<User> users3 = requestService.queryCar(request.getCart());
            for (User user1 : users3) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (!"".equals(request.getHuji())) {
            List<User> users4 = requestService.queryHuji(request.getHuji());
            for (User user1 : users4) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getMinXueli() != null) {
            List<User> users5 = requestService.queryXueli(request.getMinXueli());
            for (User user1 : users5) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getMinIncome() != null) {
            List<User> users6 = requestService.queryIncome(request.getMinIncome());
            for (User user1 : users6) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (!map.isEmpty()){
            List list1 = userMapper.selectBatchIds(map);
            return list1;
        }
        return null;
    }

    @ApiOperation("根据筛选信息展示的 id不用传")
    @PostMapping("/selectUpdate")
    public List<User> selectRequest(@RequestBody Request request) {
        User user = requestService.queryUser(request.getUid());
        List<User> list = new ArrayList<>();

//        System.out.println(user.getSex().equals("女"));
        Set map = new HashSet();
        if ("女".equals(user.getSex())){
            List<User> sex = requestService.querySex("男");
            for (User sex1 : sex) {
                map.add(sex1.getId());
            }
        }else {
            List<User> sex = requestService.querySex("女");
            for (User sex1 : sex) {
                map.add(sex1.getId());
            }
        }
        if (request.getMaritalStatus()!=null) {
            List<User> users = requestService.queryWedding(request.getMaritalStatus());
            for (User user1 : users) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getMinAge() != null && request.getMaxAge() != null) {
            List<User> users0 = requestService.queryAge(request.getMinAge(), request.getMaxAge());
            for (User user1 : users0) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getMinHeight() != null && request.getMaxHeight() != null) {
            List<User> users1 = requestService.queryHeight(request.getMinHeight(), request.getMaxHeight());
            for (User user1 : users1) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getHouse()!=null) {
            List<User> users2 = requestService.queryHouse(request.getHouse());
            for (User user1 : users2) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getCart()!=null) {
            List<User> users3 = requestService.queryCar(request.getCart());
            for (User user1 : users3) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getHuji()!=null) {
            List<User> users4 = requestService.queryHuji(request.getHuji());
            for (User user1 : users4) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getMinXueli() != null) {
            List<User> users5 = requestService.queryXueli(request.getMinXueli());
            for (User user1 : users5) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (request.getMinIncome() != null) {
            List<User> users6 = requestService.queryIncome(request.getMinIncome());
            for (User user1 : users6) {
                if (map.contains(user1.getId())){
                    list.add(user1);
                }
            }
            map.clear();
            for (User user1 : list) {
                map.add(user1.getId());
            }
            list.clear();
        }
        if (!map.isEmpty()){
            List list1 = userMapper.selectBatchIds(map);
            return list1;
        }
        return null;
    }

}

